package com.uwo.house.service.serviceImpl;

import com.uwo.house.beans.request.DesignProductionReq;
import com.uwo.house.beans.request.DesignSchemeReq;
import com.uwo.house.beans.request.MeasureCenterReq;
import com.uwo.house.beans.request.MeasureCenterSurveyReq;
import com.uwo.house.beans.response.OrderDesignDetail;
import com.uwo.house.dao.*;
import com.uwo.house.service.IDesignCenterService;
import com.uwo.house.util.ImportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * Created by PHS on 2017/7/22.
 */
@Service
public class DesignCenterService implements IDesignCenterService {

    @Autowired
    private DesignOrderStatusDao designOrderStatusDao;

    @Autowired
    private MeasureCenterSurveyDao measureCenterSurveyDao;

    @Autowired
    private MeasureCenterDao measureCenterDao;

    @Autowired
    private DesignSchemeDao designSchemeDao;

    @Autowired
    private DesignProductionDao designProductionDao;

    @Autowired
    private  ImportExcel importExcel;

    public int reviceOrder(Integer orderStatus,String orderId, String userId) {
        return designOrderStatusDao.updateOrderStatus(orderStatus,orderId, userId);
    }

    public MeasureCenterSurveyReq showFormData(String orderId) {
        return measureCenterSurveyDao.showFormData(orderId);
    }

    public MeasureCenterReq getOrderInfo(String orderId) {
        return measureCenterDao.getOrderInfo(orderId);
    }

    public MeasureCenterReq getOrderSpecialInfo(String orderId) {
        return measureCenterDao.getOrderSpecialInfo(orderId);
    }

    @Transactional
    public int addDesignScheme(DesignSchemeReq designSchemeReq) throws IOException {
        if (null != designSchemeReq) {
            String orderId = designSchemeReq.getOrderId();
            String userId = designSchemeReq.getUserId();
            String materialImgUrl = designSchemeReq.getMaterialImg();
            //String url = "http://120.27.151.238:8080/uwo-house/" + materialImgUrl;
            String url = "http://122.237.100.110:80/uwo-house/" + materialImgUrl;
            // 设计师上传设计方案 更新上传者assign_flag标志
            int result1 = designOrderStatusDao.updateAssignFlag(orderId,userId);
            // 设计师上传方案
            int result2 = designSchemeDao.addDesignScheme(designSchemeReq);
            Integer planId = designSchemeReq.getId();
            // 解析excel
            int result3 = importExcel.loadExcel(url,planId);
            if (result1 > 0 && result2 > 0 && result3 > 0) {
                return 1;
            }
        }
        return 0;
    }

    public DesignSchemeReq findDesignScheme(String orderId, String userId) {
        return designSchemeDao.findDesignScheme(orderId, userId);
    }

    public int updateDesignScheme(String orderId, String userId) {
        return designSchemeDao.updateDesignScheme(orderId, userId);
    }

    public int deleteOrderStatus(String orderId, String userId) {
        return designOrderStatusDao.deleteOrderStatus(orderId,userId);
    }

    public List<DesignProductionReq> getAllProduction(String str) {
        return designProductionDao.getAllProduction(str);
    }

    public DesignProductionReq getProductionDetail(Integer id) {
        return designProductionDao.getProductionDetail(id);
    }

    public List<DesignProductionReq> getProduction(String userId) {
        return designProductionDao.getProduction(userId);
    }

    public int deleteProduction(Integer id) {
        return designProductionDao.deleteProduction(id);
    }

    public DesignProductionReq getProductionById(Integer id) {
        return designProductionDao.getProductionById(id);
    }

    public int updateProduction(DesignProductionReq designProductionReq) {
        return designProductionDao.updateProduction(designProductionReq);
    }

    public int insertProduction(DesignProductionReq designProductionReq) {
        return designProductionDao.insertProduction(designProductionReq);
    }

    public OrderDesignDetail getOrderDataDetail(Integer id){
        return designProductionDao.getOrderDataDetail(id);
    }

}
